<!--- 
LICENSE INFORMATION:

Copyright 2007, Hem Talreja
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance with the License. 

You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License.

VERSION INFORMATION:	(0.1)

 --->
<cfcomponent output="false" displayname="database controller" hint="The wrapper for all database calls" extends="dbHelper">
	
	<cffunction name="init" access="public" returntype="dbController" output="false" hint="initializer function">
		<cfargument name="dbProperties" type="any" required="true" />
		<cfargument name="dbMetaLoader" type="any" required="true" />
		<cfargument name="dbReader" type="any" required="true" />
		<cfargument name="dbWriter" type="any" required="true" />
		
		<cfset setDBProperties(Arguments.dbProperties) />
		<cfset setDBMetaLoader(Arguments.dbMetaLoader) />
		<cfset setDBReader(Arguments.dbReader) />
		<cfset setDBWriter(Arguments.dbWriter) />
		
		<cfreturn this />
	</cffunction>
	
	
	<cffunction name="ReloadMetaExceptions" access="public" returntype="void" hint="A process to reload the meta exception file.">
		<cfset getMetaLoader().getDBMetaRetreiver().getDBMetaExceptions().loadExceptions() />
	</cffunction>
	
	
	<cffunction name="LookUP" access="public" returntype="any" output="false" hint="look up values in the database">
		<cfargument name="SelectClause" required="true" type="string" hint="The select clause for the query" />
		<cfargument name="FromTable" required="true" type="string" hint="The table to select from" />
		<cfargument name="WhereClause" required="false" default="" type="string" hint="The table to select from" />
		<cfargument name="DefaultValue" required="false" default="" type="string" hint="The value to return if no record was fount" />
		
		<cfreturn getDBReader().LookUP(ArgumentCollection=Arguments) />
	</cffunction>
	
	
	
	<cffunction name="Select" access="public" returntype="query" output="false" hint="select values from the database">
		<cfargument name="SelectClause" required="true" type="string" hint="The select clause for the query" />
		<cfargument name="FromClause" required="true" type="string" hint="The table to select from" />
		<cfargument name="WhereClause" required="false" default="" type="string" hint="The table to select from" />
		<cfargument name="OrderByClause" required="false" default="" type="string" hint="OrderBy clause" />
		<cfargument name="MaxRows" required="true" default="0" type="numeric" />
		<cfargument name="CachedWithin" required="false" type="string" default="" />
		<cfargument name="QueryName" required="false" type="string" default="selRecords" />
		
		
		<cfreturn getDBReader().Select(ArgumentCollection=Arguments) />
	</cffunction>
		


	<cffunction name="dbChange" access="public" returntype="struct" hint="update values against the database">
		<cfargument name="FieldValues" type="struct" required="yes" hint="Colmun fields and their values" />
		<cfargument name="TableName" type="string" required="yes" hint="Name of the Table to update the columns" />
		<cfargument name="PrimaryKey" type="string" required="No" default="" hint="Primary Key of the table" />
		<cfargument name="CounterKey" type="string" required="No" default="" hint="Auto Increment ID for this Table" />
		<cfargument name="ActionPermited" type="string"  required="No" default="Any" hint="The Action you want the system to perform [Any,Insert,Update,None]">
		
		<cfset var Local = StructNew() />
		
		<cfset Local.stcParams = Arguments />
		<cfset Local.stcParams.FieldStruct = getDBMetaLoader().getTableColumns(Arguments.TableName) />
	
		<cfif Arguments.PrimaryKey IS "">
			<cfset Local.stcParams.PrimaryKey = getDBMetaLoader().getTablePrimaryKey(Arguments.TableName) />
		</cfif>
	
		<cfif Arguments.CounterKey IS "">
			<cfset Local.stcParams.CounterKey = getDBMetaLoader().getTableCounterKey(Arguments.TableName) />
		</cfif>
		
		<cfreturn getDBWriter().dbChange(ArgumentCollection = Local.stcParams) />	
	</cffunction>

</cfcomponent>